package com.example.jpushdemo;

import cn.jpush.android.api.JPushInterface;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.webview.GetData;
import com.webview.MyWebChromeClient;
import com.webview.MyWebClient;
import com.zhong.mygitnewsapp.R;

import org.json.JSONException;
import org.json.JSONObject;

public class TestActivity extends AppCompatActivity implements GetData {

    private ProgressBar mProgressBar;
    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weblink);
        setTitle("加载中...");
        mWebView = (WebView) findViewById(R.id.my_web);
        mProgressBar = (ProgressBar) findViewById(R.id.my_web_progress);
//        TextView tv = new TextView(this);
//        tv.setText("用户自定义打开的Activity");
        Intent intent = getIntent();
        if (null != intent) {
            Bundle bundle = getIntent().getExtras();
//            //通过JPushInterface可以获取所有的字段
//	        String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
//	        String content = bundle.getString(JPushInterface.EXTRA_ALERT);
//	        tv.setText("Title : " + title + "  " + "Content : " + content);

            //获取附加字段，字符串，格式是JSON
            String json = bundle.getString(JPushInterface.EXTRA_EXTRA);
            try {
                //后台设置的字段
                JSONObject object = new JSONObject(json);
                String url = object.optString("url");
                if (url != null) {
                    mWebView.setWebViewClient(new MyWebClient(this));
                    mWebView.setWebChromeClient(new MyWebChromeClient(this));
                    WebSettings settings = mWebView.getSettings();
                    settings.setJavaScriptEnabled(true);
                    settings.setAllowFileAccess(true);
                    settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
                    mWebView.loadUrl(url);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
//        addContentView(tv, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    }

    @Override
    public void onBackPressed() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        mProgressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        mProgressBar.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        mProgressBar.setProgress(newProgress);
    }

    @Override
    public void onReceivedTitle(WebView view, String title) {
        if (title != null) {
            setTitle(title);
        }
    }
}
